package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.auth.bean.AuthMerchantOutput;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.business.auth.contract.AuthMerchantContract;
import com.tianyang.yukuaifu.business.auth.model.AuthConverter;
import com.tianyang.yukuaifu.business.auth.presenter.AuthMerchantPresenter;
import com.tianyang.yukuaifu.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifu.common.bean.TabEntity;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.tablelayout.CommonTabLayout;
import com.tianyang.yukuaifu.module.tablelayout.CustomTabEntity;
import com.tianyang.yukuaifu.module.tablelayout.listener.OnTabSelectListener;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;
import com.tianyang.yukuaifu.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifu.utils.permissions.PermissionsMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：小微商户进件
 * Created by xyj on 2024/3/9
 */
public class AuthMerchantActivity extends BaseEditActivity implements AuthMerchantContract.View {

    private String[] permissions = new String[]{PermissionsConstant.CAMERA, PermissionsConstant.WRITE_STORAGE, PermissionsConstant.READ_STORAGE};

    @BindView(R.id.tabLayout)
    CommonTabLayout vTabLayout;
    @BindView(R.id.authBasicView)
    AuthMerchantBasicView authBasicView;
    @BindView(R.id.authLegalView)
    AuthMerchantLegalView authLegalView;
    @BindView(R.id.placeCodeTextTv)
    TextView placeCodeTextTv;

    private AuthMerchantContract.Presenter mPresenter;

    private int mCurrentIndex = 0;

    private boolean isOnlyRead = false;

    private String currentPhotoType, posChannel;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_auth_merchant;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("小微商户进件");
        commonTitle.showRightTextView("保存");
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        mTabEntities.add(new TabEntity("商户信息", 0, 0));
        mTabEntities.add(new TabEntity("店主信息", 0, 0));
        vTabLayout.setTabData(mTabEntities);
        vTabLayout.setCurrentTab(mCurrentIndex);

        String userName = getIntent().getStringExtra(AuthConfig.INTENT_USERNAME);
        isOnlyRead = getIntent().getBooleanExtra(AuthConfig.INTENT_ONLY_READ, false);
        String posStatus = getIntent().getStringExtra(AuthConfig.INTENT_POS_STATUS);
        boolean isAgent = getIntent().getBooleanExtra(AuthConfig.INTENT_IS_AGENT, false);
        mPresenter = new AuthMerchantPresenter(this, userName, posStatus, isAgent);
        if (isOnlyRead) {
            authBasicView.setOnlyReadView();
            authLegalView.setOnlyReadView();
        } else {
            commonTitle.showRightTextView("保存");
        }
    }

    @OnClick({R.id.nextStepTv, R.id.confirmTv, R.id.shopHeadImgRfl, R.id.shopOutImgRfl, R.id.shopDeskImgRfl, R.id.otherAttachmentImgRfl,
            R.id.idCardFrontImgRfl, R.id.idCardBackImgRfl, R.id.handImgRfl, R.id.bankFrontImgRfl, R.id.industryTypeOneTv,
            R.id.industryAliPayTv, R.id.industryWechatTv, R.id.placeCodeTextTv, R.id.openingBankTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        if (isOnlyRead) return;
        switch (view.getId()) {
            case R.id.nextStepTv:
                if (authBasicView.checkMerchantBasic()) {
                    saveIdCertTemp(false, true);
                }
                break;
            case R.id.confirmTv:
                if (authBasicView.checkMerchantBasic() && authLegalView.checkMerchantLegal()) {
                    saveIdCertTemp(true, false);
                }
                break;
            case R.id.shopHeadImgRfl:
                currentPhotoType = CommonConfig.TYPE_SHOPHEAD;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.shopOutImgRfl:
                currentPhotoType = CommonConfig.TYPE_SHOPOUT;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.shopDeskImgRfl:
                currentPhotoType = CommonConfig.TYPE_SHOPDESK;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.otherAttachmentImgRfl:
                currentPhotoType = CommonConfig.TYPE_OTHER_ATTACHMENT;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.idCardFrontImgRfl:
                currentPhotoType = CommonConfig.TYPE_IDFROPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.idCardBackImgRfl:
                currentPhotoType = CommonConfig.TYPE_IDCONPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.handImgRfl:
                currentPhotoType = CommonConfig.TYPE_IDHANDPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.bankFrontImgRfl:
                currentPhotoType = CommonConfig.TYPE_CRDFROPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.industryTypeOneTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, AuthConfig.MCH_TYPE_SMALL,
                        AuthConfig.MCC_TYPE_BUSINESS, posChannel, CommonConfig.REQUESTCODE_AUTH_BUSINESS);
                break;
            case R.id.industryAliPayTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, AuthConfig.MCH_TYPE_SMALL,
                        AuthConfig.MCC_TYPE_ALI, posChannel, CommonConfig.REQUESTCODE_AUTH_BUSINESS_ALI);
                break;
            case R.id.industryWechatTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, AuthConfig.MCH_TYPE_SMALL,
                        AuthConfig.MCC_TYPE_WX, posChannel, CommonConfig.REQUESTCODE_AUTH_BUSINESS_WX);
                break;
            case R.id.placeCodeTextTv:
                mPresenter.queryProvince(view, placeCodeTextTv.getText().toString());
                break;
            case R.id.openingBankTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, AuthConfig.MCH_TYPE_SMALL,
                        AuthConfig.MCC_TYPE_BANK, posChannel, CommonConfig.REQUESTCODE_AUTH_BUSINESS_BANK);
                break;
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveIdCertTemp(false, false);
            }
        });
        vTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (mCurrentIndex != position) {
                    mCurrentIndex = position;
                    showContentView();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void saveIdCertTemp(boolean isAuth, boolean isNextStep) {
        SoftInputUtil.hideSoftInput(AuthMerchantActivity.this);
        Map<String, String> map = AuthConverter.getInstance().getAuthMerchantInput(
                authBasicView.getMerchantBasicInput(), authLegalView.getMerchantLegalInput());
        mPresenter.saveIdCertTemp(map, isAuth, isNextStep, posChannel);
    }

    @Override
    public void showNextStepView() {
        mCurrentIndex = 1;
        vTabLayout.setCurrentTab(mCurrentIndex);
        showContentView();
    }

    @Override
    public void showMerchantInfoView(AuthMerchantOutput output) {
        if (output != null) {
            posChannel = output.getPosChannel();
            authBasicView.onFeedBackView(output, posChannel);
            authLegalView.onFeedBackView(output);
        }
    }

    @Override
    public void showProvCityAreaView(String provCityName, String wheelCode) {
        placeCodeTextTv.setText(VerifyUtil.checkString(provCityName));
        authBasicView.showProvCityAreaView(provCityName, wheelCode);
    }

    @Override
    public void showPhotoView(String urlStr, String photoType) {
        if (mCurrentIndex == 0) {
            authBasicView.showPhotoView(urlStr, photoType);
        } else {
            authLegalView.showPhotoView(urlStr, photoType);
        }
    }

    @Override
    public void showIdCardView(IdCardOutput output) {
        authLegalView.showIdCardInfoView(output);
    }

    @Override
    public void showBankCardView(BankRegOutput output) {
        authLegalView.showBankCardInfoView(output);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_AUTH_BUSINESS) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                authBasicView.showBusinessView((BusinessOutput) data
                        .getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO), AuthConfig.MCC_TYPE_BUSINESS);
            }
        } else if (requestCode == CommonConfig.REQUESTCODE_AUTH_BUSINESS_ALI) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                authBasicView.showBusinessView((BusinessOutput) data
                        .getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO), AuthConfig.MCC_TYPE_ALI);
            }
        } else if (requestCode == CommonConfig.REQUESTCODE_AUTH_BUSINESS_WX) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                authBasicView.showBusinessView((BusinessOutput) data
                        .getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO), AuthConfig.MCC_TYPE_WX);
            }
        } else if (requestCode == CommonConfig.REQUESTCODE_AUTH_BUSINESS_BANK) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                authLegalView.showBusinessView((BusinessOutput) data
                        .getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO));
            }
        } else {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPermissionGranted(List<String> permission) {
        if (PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
            mPresenter.showSelectImgSourceView(currentPhotoType);
        }
    }

    /**
     * Description: 显示内容
     */
    private void showContentView() {
        switch (mCurrentIndex) {
            case 0:
                authBasicView.setVisibility(View.VISIBLE);
                authLegalView.setVisibility(View.GONE);
                break;
            case 1:
                authBasicView.setVisibility(View.GONE);
                authLegalView.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AuthMerchantContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (authBasicView != null) {
            authBasicView.onDestroyView();
        }
        if (authLegalView != null) {
            authLegalView.onDestroyView();
        }
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
